#include <iostream>
#include "bitset.hpp"
using namespace std;

void test(){
  zty::bitset<100> bt;
  cout << bt.count() << endl;
  cout << bt.size() << endl;
  bt.set(0);
  bt.set(8);
  bt.set(9);
  bt.set(20);
  cout << bt.count() << endl;
  cout << bt.size() << endl;
  cout << bt.test(0) << endl;
  cout << bt.test(8) << endl;
  cout << bt.test(9) << endl;
  cout << bt.test(20) << endl;
  bt.reset(0);
  bt.reset(8);
  bt.reset(9);
  bt.reset(20);
  cout << bt.count() << endl;
  cout << bt.size() << endl;
  cout << bt.test(0) << endl;
  cout << bt.test(8) << endl;
  cout << bt.test(9) << endl;
  cout << bt.test(20) << endl;
}

void test1(){
  int arr[] = {6,99,1,1,2,4,3,5,6,7,1,3,2,4,7,9,8,5,4,3,21,8,9,4,2,3,1,4,3,57};
  zty::twobitset1<100> tbs;
  for(int e : arr){
    tbs.set(e);
  }
  tbs.print_once_num();
}

void test2(){
  int arr1[] = {6,99,1,1,2,4,3,5,6,7,1,3,2,4,7,9,8,5,4,3,21,8,9,4,2,3,1,4,3,57};
  int arr2[] = {5,3,1,5,12,23,45,6,78,9,12,13,15,57,3,4,9};
  zty::twobitset2<100> tbs;
  for(int e : arr1){
    tbs.setbs1(e);
  }
  for(int e : arr2){
    tbs.setbs2(e);
  }
  tbs.print_intersection_set();
}

void test3(){
  int arr[] = {1,1,1,2,2,2,3,4,4,4,4,4,5,5,6};
  zty::twobitset3<100> tbs;
  for(int e : arr){
    tbs.set(e);
  }
  tbs.print_atmost_twice();
}

void TestBloomFilter1()
{
  zty::BloomFilter<11> bf;
	string arr1[] = { "苹果", "西瓜", "阿里", "美团", "苹果", "字节", "西瓜", "苹果", "香蕉", "苹果", "腾讯" };

	for (auto& str : arr1)
	{
		bf.set(str);
	}

	for (auto& str : arr1)
	{
		cout << bf.test(str) << endl;
	}
	cout << endl << endl;

	string arr2[] = { "苹果111", "西瓜", "阿里2222", "美团", "苹果dadcaddxadx", "字节", "西瓜sSSSX", "苹果", "香蕉", "苹果$", "腾讯" };

	for (auto& str : arr2)
	{
		cout <<str<<":"<<bf.test(str) << endl;
	}
}

void TestBloomFilter2()
{
	srand(time(0));
	const size_t N = 100000;
  zty::BloomFilter<N> bf;
  cout << "sizeof BloomFilter: " << sizeof(bf) << endl; 
	std::vector<std::string> v1;
	std::string url = "https://www.cnblogs.com/-clq/archive/2012/05/31/2528153.html";

	for (size_t i = 0; i < N; ++i)
	{
		v1.push_back(url + std::to_string(1234 + i));
	}

	for (auto& str : v1)
	{
		bf.set(str);
	}

	// 相似字符串
	std::vector<std::string> v2;
	for (size_t i = 0; i < N; ++i)
	{
		std::string url = "http://www.cnblogs.com/-clq/archive/2021/05/31/2528153.html";
		url += std::to_string(rand() + i);
		v2.push_back(url);
	}

	size_t n2 = 0;
	for (auto& str : v2)
	{
		if (bf.test(str))
		{
			++n2;
		}
	}
	cout << "相似字符串误判率:" << (double)n2 / (double)N << endl;

  //不相似字符串
	std::vector<std::string> v3;
	for (size_t i = 0; i < N; ++i)
	{
		string url = "zhihu.com";
		url += std::to_string(rand()+i);
		v3.push_back(url);
	}

	size_t n3 = 0;
	for (auto& str : v3)
	{
		if (bf.test(str))
		{
			++n3;
		}
	}
	cout << "不相似字符串误判率:" << (double)n3 / (double)N << endl;
}

int main(){
  //test();
  //test1();
  //test2();
  //test3();
  //TestBloomFilter1();  
  TestBloomFilter2();
  return 0;
}
